package cc.telecomdigital.tdstock.adapter;

import java.util.List;

import cc.telecomdigital.tdstock.R;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OilQuoteItemAdapter extends BaseAdapter
{
	private LayoutInflater			minflater;
	private List<OilQuoteViewHolder>	list;

	public OilQuoteItemAdapter(LayoutInflater inflater,
			List<OilQuoteViewHolder> list)
	{
		super();
		this.minflater = inflater;
		this.list = list;
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return this.list.size();
	}

	@Override
	public Object getItem(int position)
	{
		return position < this.list.size() ? this.list.get(position) : null;
	}

	@Override
	public long getItemId(int position)
	{
		return position < this.list.size() ? this.list.get(position).id : -1l;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		OilQuoteViewHolder commHolder = null;
		if (convertView == null)
		{
			convertView = minflater.inflate(R.layout.oil_quote_list_item,
					null);
			if(position == getCount()-1)
			{
				ViewGroup gp = (ViewGroup)convertView;
				gp.removeViewAt(gp.getChildCount()-1);
			}
			commHolder = new OilQuoteViewHolder();
			commHolder.date = (TextView) convertView
					.findViewById(R.id.oil_date);
			commHolder.value = (TextView) convertView
					.findViewById(R.id.oil_value);
			commHolder.net = (TextView) convertView
					.findViewById(R.id.oil_net);
			convertView.setTag(commHolder);
		}
		else
		{
			commHolder = (OilQuoteViewHolder) convertView.getTag();
		}
		commHolder.date.setText(this.list.get(position).date.getText());
		commHolder.value.setText(this.list.get(position).value.getText());
		commHolder.net.setText(this.list.get(position).net.getText());
		if (this.list.get(position).net.getText() != null
				&& this.list.get(position).net.getText().toString()
						.indexOf("-") != -1)
		{
			commHolder.value.setTextColor(Color.RED);
			commHolder.net.setTextColor(Color.RED);
		}
		return convertView;
	}

}
